#!/usr/bin/python

import util
import lib

from optparse import OptionParser
import sys
import shutil

"""
The build() function makes the following changes to the V8 src dir:

NEW FILES (copied to V8 /src from ecmascript-ast/src):

- json-ast-token.h/cc - via SConscript

- json-ast-builder.h/cc - via #include in prettyprinter.h/cc

- json-ast-parser.h/cc - via #include in parser.h/cc

(N.B - files in ecmascript-ast/src prefixed with _ e.g _json-delay.js 
are not copied to the src dir but rather directly 'injected' into the 
corresponding file e.g json_delay.js.
They are:
_json-delay.js
_runtime.h
_runtime.cc
Note: they could be #include'd rather than injected).


UPDATED FILES (which are backed up to the ecmascript-ast-bak dir):

- SConscript. Add new .cc files:
    - json-ast-token.cc

- parser.h 
    add #include json-ast-parser.h (for MakeASTJsonML())

- parser.cc 
    add #include json-ast-token.h - at the top include's
    add #include json-ast-parser.cc - at the bottom.
    add HACK to add Id AST node's for "var x" to init Block ast nodes
    
- compiler.h
    fix enum ValidationState - JSON_AST

- compiler.cc
    fix MakeFunction() - use enum JSON_AST to call MakeASTJsonML()
   
- json-delay.js
    inject _json_delay.js

- runtime.h 
    - add #include prettyprinter.h // OR json-ast-builder.h
    - inject _runtime.h
                                                 
- runtime.cc
    - inject _runtime.cc
    - fix Runtime_CompileString() to use is_json argument
      set to null to the JSON_AST value
    
- prettyprinter.h
    - add #include json-ast-token.h
    - add #include json-ast-builder.h
    - private to protected in JsonAstBuilder class
    
- prettyprinter.cc
    - add #include json-ast-builder.cc
    
N.B: The build script reads unaltered files from the bak dir. 
This means this script can be run repeatedly.
"""


def get_v8_dir():

    parser = OptionParser()
    parser.add_option("-d", "--dir", help="set the target v8 dir in which ecmascript-ast will be installed", default="..")
    (options, args) = parser.parse_args()

    the_dir = options.dir
    #print the_dir
    return the_dir


def build():
 
    print("*** Start ***")
    
    the_v8_dir = get_v8_dir()

    # Fix the v8 dir to update by uncommenting and setting this var
    #the_v8_dir = "../v8test"
    
    # double check that the v8_dir has been set
    if the_v8_dir == None or the_v8_dir.strip() == "":
        sys.exit("Error: the_v8_dir var has not been set")
    
    # set the v8 dir. If dir not found the script will terminate.
    util.set_v8_dir(the_v8_dir)
    
    # copy files to bak dir.
    util.backup_files()

    # copy NEW files to the v8 src dir
    util.copy_file_to_src("json-ast-token.h")
    util.copy_file_to_src("json-ast-token.cc")
    util.copy_file_to_src("json-ast-builder.h")
    util.copy_file_to_src("json-ast-builder.cc")
    util.copy_file_to_src("json-ast-parser.h")
    util.copy_file_to_src("json-ast-parser.cc")
  
    # UPDATE files
    print "--- Updating files in src dir: %s " %(','.join(util._files))
    # inject
    lib.update_json_delay_js()
    lib.update_runtime_h()
    lib.update_runtime_cc()
    # fix
    lib.update_SConscript()
    lib.update_compiler_h()
    lib.update_compiler_cc()
    # include and fix
    lib.update_parser_h()
    lib.update_parser_cc()
    lib.update_prettyprinter_h()
    lib.update_prettyprinter_cc()
    
    # Copy the dir of tests to the v8 dir
    util.copy_test_dir()
    
    # Copy the shell scripts and example.js
    shutil.copy("example.js", the_v8_dir)
    shutil.copy("ast.sh", the_v8_dir)
    shutil.copy("ast-parse.sh", the_v8_dir)
    shutil.copy("ast-evaluate.sh", the_v8_dir)
    shutil.copy("astd8.js", the_v8_dir)
    
    print("*** End ***")
    
# main function
build()
